import RequestObject from "./requestObject";
import request from "@/utils/request"
import { MessageBox } from "element-ui"

/**
 *新增数据
 *
 * @param {*} addModel 需要新增的数据模型
 * @param {*} addModelList 需要新增的数据模型集合
 * @param {*} apiUrl API Url
 * @return {*} Promise对象
 */
function Add(addModel, addModelList, apiUrl) {
    return new Promise(function (resolve, reject) {
        let dataObject = RequestObject.CreatePostObject(addModel, addModelList)
        let reqObject = {
            url: apiUrl,
            method: "post",
            data: dataObject
        };
        request(reqObject)
            .then(res => {
                if (res.code == -1) {
                    reject(res.info);
                } else {
                    resolve("新增数据成功")
                }
            })
            .catch(error => {
                reject(error);
            })
    });
}

/**
 *新增数据，带确认对话框
 *
 * @param {*} confrimMessage 确认消息
 * @param {*} addModel 需要新增的数据模型
 * @param {*} addModelList 需要新增的数据模型集合
 * @param {*} apiUrl API Url
 * @return {*} Promise对象
 */
function AddWithConfirm(confrimMessage, addModel, addModelList, apiUrl) {
    return new Promise(function (resolve, reject) {
        if (!confrimMessage) {
            reject("删除方法调用参数错误，请传入confrimMessage参数");
        }
        MessageBox.confirm(confrimMessage, '确认对话框', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            showConfirmButton: true
        }).then(() => {
            if (!confrimMessage) {
                reject("删除方法调用参数错误，请传入confrimMessage参数");
            }
            let dataObject = RequestObject.CreatePostObject(addModel, addModelList)
            let reqObject = {
                url: apiUrl,
                method: "post",
                data: dataObject
            };
            request(reqObject)
                .then(res => {
                    if (res.code == -1) {
                        reject(res.info);
                    } else {
                        resolve(res)
                    }
                })
                .catch(error => {
                    reject(error);
                })
        });
    });
}

/**
 *编辑数据
 *
 * @param {*} editModel 需要编辑的数据模型
 * @param {*} editModelList 需要编辑的数据模型集合
 * @param {*} apiUrl API Url
 * @return {*} Promise对象
 */
function Edit(editModel, editModelList, apiUrl) {
    return new Promise(function (resolve, reject) {
        let dataObject = RequestObject.CreatePostObject(editModel, editModelList)
        let reqObject = {
            url: apiUrl,
            method: "put",
            data: dataObject
        };
        request(reqObject)
            .then(res => {
                if (res.code == -1) {
                    reject(res.info);
                } else {
                    resolve("编辑数据成功")
                }
            })
            .catch(error => {
                reject(error);
            })
    });
}

/**
 *编辑数据，带确认对话框
 *
 * @param {*} confrimMessage 确认消息
 * @param {*} editModel 需要编辑的数据模型
 * @param {*} editModelList 需要编辑的数据模型集合
 * @param {*} apiUrl API Url
 * @return {*} Promise对象
 */
function EditWithConfirm(confrimMessage, editModel, editModelList, apiUrl) {
    return new Promise(function (resolve, reject) {
        if (!confrimMessage) {
            reject("删除方法调用参数错误，请传入confrimMessage参数");
        }
        MessageBox.confirm(confrimMessage, '确认对话框', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            showConfirmButton: true
        }).then(() => {
            if (!confrimMessage) {
                reject("删除方法调用参数错误，请传入confrimMessage参数");
            }
            let dataObject = RequestObject.CreatePostObject(editModel, editModelList)
            let reqObject = {
                url: apiUrl,
                method: "put",
                data: dataObject
            };
            request(reqObject)
                .then(res => {
                    if (res.code == -1) {
                        reject(res.info);
                    } else {
                        resolve("编辑数据成功")
                    }
                })
                .catch(error => {
                    reject(error);
                })
        });
    });
}


/**
 *删除数据
 *
 * @param {*} confrimMessage 确认消息
 * @param {*} deleteDataObject 需要删除的数据
 * @param {*} deleteDataObjectList 需要删除的数量集合
 * @param {*} apiUrl API Url
 * @return {*} Promise对象
 */
function Delete(deleteModel, DeleteModelList, apiUrl) {
    return new Promise(function (resolve, reject) {
        let deleteData = RequestObject.CreatePostObject(deleteModel, DeleteModelList);
        let reqObject = {
            url: apiUrl,
            method: 'delete',
            data: deleteData
        };
        request(reqObject)
            .then(res => {
                if (res.code == -1) {
                    reject(res.info);
                } else {
                    resolve("删除数据成功");
                }
            }).catch(err => {
                console.log("调用删除方法发生错误，错误信息：" + err);
                reject(err);
            })
    });
}

/**
 *删除数据，带去人对话框
 *
 * @param {*} confrimMessage 确认消息
 * @param {*} deleteDataObject 需要删除的数据
 * @param {*} deleteDataObjectList 需要删除的数量集合
 * @param {*} apiUrl API Url
 * @return {*} Promise对象
 */
function DeleteWithConfirm(confrimMessage, deleteDataObject, deleteDataObjectList, apiUrl) {
    return new Promise(function (resolve, reject) {
        if (!confrimMessage) {
            reject("删除方法调用参数错误，请传入confrimMessage参数");
        }
        MessageBox.confirm(confrimMessage, '确认对话框', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            showConfirmButton: true
        }).then(() => {
            let deleteData = RequestObject.CreatePostObject(deleteDataObject, deleteDataObjectList);
            let reqObject = {
                url: apiUrl,
                method: 'delete',
                data: deleteData
            };
            request(reqObject)
                .then(res => {
                    if (res.code == -1) {
                        reject(res.info);
                    } else {
                        resolve("删除数据成功");
                    }
                }).catch(err => {
                    console.log("调用删除方法发生错误，错误信息：" + err);
                    reject(err);
                })
        })
    });


}

/**
 *查询数据
 *
 * @param {*} reqObject 查询参数
 * @param {*} apiUrl API Url
 * @return {*} Promise对象
 */
function Query(reqObject, apiUrl) {
    return new Promise(function (resolve, reject) {
        let requestObj = {
            url: apiUrl,
            method: 'get',
            params: {
                requestObject: JSON.stringify(reqObject)
            }
        };
        request(requestObj)
            .then(res => {
                if (res.code === 0) {
                    resolve(res);
                } else {
                    reject(res.info)
                }
            }).catch(err => {
                reject(err);
            })
    });
}



/**
 *查询数据
 *
 * @param {*} apiUrl API Url
 * @return {*} Promise对象
 */
function Get(apiUrl) {
    return new Promise(function (resolve, reject) {
        let requestObj = {
            url: apiUrl,
            method: 'get'
        };
        request(requestObj)
            .then(res => {
                if (res.code === 0) {
                    resolve(res);
                } else {
                    reject(res.info)
                }
            }).catch(err => {
                reject(err);
            })
    });
}

/**
 *查询数据
 *
 * @param {*} apiUrl API Url
 * @return {*} Promise对象
 */
function GetWithParams(apiUrl, params) {
    return new Promise(function (resolve, reject) {
        let requestObj = {
            url: apiUrl,
            method: 'get',
            params: params
        };
        request(requestObj)
            .then(res => {
                if (res.code === 0) {
                    resolve(res);
                } else {
                    reject(res.info)
                }
            }).catch(err => {
                reject(err);
            })
    });
}

/**
 *编辑数据
 *
 * @param {*} editModel 需要编辑的数据模型
 * @param {*} editModelList 需要编辑的数据模型集合
 * @param {*} apiUrl API Url
 * @return {*} Promise对象
 */
function Put(editModel, editModelList, apiUrl) {
    return new Promise(function (resolve, reject) {
        let dataObject = RequestObject.CreatePostObject(editModel, editModelList)
        let reqObject = {
            url: apiUrl,
            method: "put",
            data: dataObject
        };
        request(reqObject)
            .then(res => {
                resolve(res)
            })
            .catch(error => {
                reject(error);
            })
    });
}

const CURDHelper = {
    Add: Add,
    AddWithConfirm: AddWithConfirm,
    Edit: Edit,
    EditWithConfirm: EditWithConfirm,
    Delete: Delete,
    DeleteWithConfirm: DeleteWithConfirm,
    Query: Query,
    Put: Put,
    Get: Get,
    GetWithParams: GetWithParams
}

export default CURDHelper;